package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;
import com.zmx.patientend.view.util.ViewStar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/7/30  17:14
 * 功 能：评价
 */
public class OrderEvaluationActivity extends BaseActivity {


    @BindView(R.id.ratingbar_a)
    ViewStar ratingbarA;
    @BindView(R.id.ratingbar_b)
    ViewStar ratingbarB;
    @BindView(R.id.ratingbar_c)
    ViewStar ratingbarC;
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.describe_a)
    TextView describeA;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.describe_b)
    TextView describeB;
    @BindView(R.id.describe_c)
    TextView describeC;
    @BindView(R.id.submit)
    Button submit;

    private String ask_id,type;

    private int service_points = 0;
    private int speed_points = 0;
    private int solve_points = 0;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_order_evaluation;

    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        ask_id = this.getIntent().getStringExtra("ask_id");
        type = this.getIntent().getStringExtra("type");

        if (type == null){

            name.setText("对该医生评价");

        }else{

            name.setText("对该专家团队评价");
        }

        ratingbarA.setOnRatingChangeListener(new ViewStar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {

                Log.e("多少颗星星", "多少颗星星" + ratingCount);

                if (ratingCount == 5.0) {

                    describeA.setText("非常满意");
                    service_points = 100;

                } else if (ratingCount == 4.0) {

                    describeA.setText("满意");
                    service_points = 80;

                } else if (ratingCount == 3.0) {

                    describeA.setText("一般");
                    service_points = 60;

                } else if (ratingCount == 2.0) {

                    describeA.setText("差");
                    service_points = 40;

                } else if (ratingCount == 1.0) {

                    describeA.setText("非常差");
                    service_points = 20;
                }


            }
        });
        ratingbarB.setOnRatingChangeListener(new ViewStar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {

                Log.e("多少颗星星", "多少颗星星" + ratingCount);

                if (ratingCount == 5.0) {

                    describeB.setText("非常满意");
                    solve_points = 100;

                } else if (ratingCount == 4.0) {

                    describeB.setText("满意");
                    solve_points = 80;

                } else if (ratingCount == 3.0) {

                    describeB.setText("一般");
                    solve_points = 60;

                } else if (ratingCount == 2.0) {

                    describeB.setText("差");
                    solve_points = 40;

                } else if (ratingCount == 1.0) {

                    describeB.setText("非常差");
                    solve_points = 20;

                }


            }
        });
        ratingbarC.setOnRatingChangeListener(new ViewStar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {

                Log.e("多少颗星星", "多少颗星星" + ratingCount);

                if (ratingCount == 5.0) {

                    describeC.setText("非常满意");
                    speed_points = 100;

                } else if (ratingCount == 4.0) {

                    describeC.setText("满意");
                    speed_points = 80;

                } else if (ratingCount == 3.0) {

                    describeC.setText("一般");
                    speed_points = 60;

                } else if (ratingCount == 2.0) {

                    describeC.setText("差");
                    speed_points = 40;

                } else if (ratingCount == 1.0) {

                    describeC.setText("非常差");
                    speed_points = 20;
                }


            }
        });

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));

                        if (object.getInt("code") == 1){

                            Intent data = new Intent();
                            data.putExtra("state","1");
                            setResult(100,data);
                            finish();

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    @OnClick(R.id.submit)
    public void onViewClicked() {

        if (service_points == 0){

            Toast("请给服务打分");
            return;

        }
        if (speed_points == 0){

            Toast("请给回复速度打分");
            return;

        }
        if (solve_points == 0){

            Toast("请给解决问题打分");
            return;

        }
        if (editText.getText().toString().equals("")){

            Toast("评论不能为空哦");
            return;

        }

        if (type == null){

            addComment();

        }else{

            addComments();
        }
    }

    /**
     * 关闭订单
     */
    public void addComment() {

        List<String> ls = new ArrayList<>();
        ls.add(ask_id);
        ls.add(editText.getText().toString());
        ls.add(service_points+"");
        ls.add(solve_points+"");
        ls.add(speed_points+"");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "addComment");
        params.put("data[ask_id]", ask_id);
        params.put("data[content]", editText.getText().toString());
        params.put("data[service]", service_points);
        params.put("data[speed]", speed_points);
        params.put("data[solve]", solve_points);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app/", params, handler, 1, 404);

    }

    /**
     * 关闭订单
     */
    public void addComments() {

        List<String> ls = new ArrayList<>();
        ls.add(ask_id);
        ls.add(editText.getText().toString());
        ls.add(service_points+"");
        ls.add(solve_points+"");
        ls.add(speed_points+"");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "teamAddComment");
        params.put("data[ask_id]", ask_id);
        params.put("data[content]", editText.getText().toString());
        params.put("data[service]", service_points);
        params.put("data[speed]", speed_points);
        params.put("data[solve]", solve_points);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.team/entry", params, handler, 1, 404);

    }

}
